<?php


namespace App\Http\Controllers\queue;


class ProcessPool
{
    private $processMaxNumber;//最大进程数量
    private $processQueue;//进程池
    public function __construct()
    {
        $this->processMaxNumber = 12;//
        $this->processQueue = [];
    }

    private function deamon()
    {

    }
    //初始化进程池 单例模式
    private function init()
    {
        for($i=0;$i<$this->processMaxNumber;$i++)
        {
            $pid = pcntl_fork();
            if($pid === 0){
                echo "xxxxx";
                exit;
                break;
            }
            if($pid < 0){
                echo "创建进程出错";
                $i --;
                continue;
            }
            if($pid > 0){
                array_push($this->processQueue,$pid);
            }
        }
    }

    public function run()
    {
        $pid = getmypid();
        echo "pid:{$pid}\n";
        $this->init();
        while (1){
            $pid = getmypid();
            echo "pid:{$pid}\n";
            echo "当前进程池数量".sizeof($this->processQueue)."\n";
            var_dump($this->processQueue);
            sleep(5);

        }
    }
}

$x = new ProcessPool();
$x->run();